/**
 * 模块事件发射器实现
 */
import type { ModuleEventType, ModuleEventHandler, IModuleEventEmitter } from './types'

export class EventEmitter implements IModuleEventEmitter {
  private listeners = new Map<ModuleEventType, Set<ModuleEventHandler>>()
  private onceListeners = new Map<ModuleEventType, Set<ModuleEventHandler>>()

  /**
   * 注册事件监听器
   */
  on(event: ModuleEventType, handler: ModuleEventHandler): void {
    if (!this.listeners.has(event)) {
      this.listeners.set(event, new Set())
    }
    this.listeners.get(event)!.add(handler)
  }

  /**
   * 注册一次性事件监听器
   */
  once(event: ModuleEventType, handler: ModuleEventHandler): void {
    if (!this.onceListeners.has(event)) {
      this.onceListeners.set(event, new Set())
    }
    this.onceListeners.get(event)!.add(handler)
  }

  /**
   * 移除事件监听器
   */
  off(event: ModuleEventType, handler: ModuleEventHandler): void {
    // 移除普通监听器
    const normalListeners = this.listeners.get(event)
    if (normalListeners) {
      normalListeners.delete(handler)
      if (normalListeners.size === 0) {
        this.listeners.delete(event)
      }
    }

    // 移除一次性监听器
    const onceListeners = this.onceListeners.get(event)
    if (onceListeners) {
      onceListeners.delete(handler)
      if (onceListeners.size === 0) {
        this.onceListeners.delete(event)
      }
    }
  }

  /**
   * 发射事件
   */
  emit(event: ModuleEventType, moduleId: string, ...args: any[]): void {
    // 执行普通监听器
    const normalListeners = this.listeners.get(event)
    if (normalListeners) {
      normalListeners.forEach(handler => {
        try {
          handler(moduleId, ...args)
        } catch (error) {
          console.error(`Error in module event handler for ${event}:`, error)
        }
      })
    }

    // 执行一次性监听器
    const onceListeners = this.onceListeners.get(event)
    if (onceListeners) {
      onceListeners.forEach(handler => {
        try {
          handler(moduleId, ...args)
        } catch (error) {
          console.error(`Error in once module event handler for ${event}:`, error)
        }
      })
      // 执行后移除一次性监听器
      this.onceListeners.delete(event)
    }
  }

  /**
   * 移除所有监听器
   */
  removeAllListeners(event?: ModuleEventType): void {
    if (event) {
      this.listeners.delete(event)
      this.onceListeners.delete(event)
    } else {
      this.listeners.clear()
      this.onceListeners.clear()
    }
  }

  /**
   * 获取指定事件的监听器数量
   */
  listenerCount(event: ModuleEventType): number {
    const normalCount = this.listeners.get(event)?.size || 0
    const onceCount = this.onceListeners.get(event)?.size || 0
    return normalCount + onceCount
  }

  /**
   * 获取所有事件名称
   */
  eventNames(): ModuleEventType[] {
    const events = new Set<ModuleEventType>()
    this.listeners.forEach((_, event) => events.add(event))
    this.onceListeners.forEach((_, event) => events.add(event))
    return Array.from(events)
  }

  /**
   * 检查是否有指定事件的监听器
   */
  hasListeners(event: ModuleEventType): boolean {
    return this.listeners.has(event) || this.onceListeners.has(event)
  }
}
